﻿using DataDefine;
using TLDNs.Views;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsingEventAggregator.Core;
using Forms = System.Windows.Forms;
using HCC = HandyControl.Controls;

namespace TLDNs.ViewModels
{
    public class NoUserContentViewModel : EAViewModelBase
    {
        public NoUserContentViewModel(IContainerExtension Container, IRegionManager rm, IEventAggregator ea) : base(Container, rm, ea)
        {
            CurLanguage = Properties.Settings.Default.DefaultLanguage;
            _ea.GetEvent<OpenSFMEvent>().Subscribe(OpenSFMFileCommand_Sub);
        }

        public DelegateCommand<string> ChangeLanguageCommand => new DelegateCommand<string>(ChangeLanguageCommand_Sub);
        private void ChangeLanguageCommand_Sub(string lang)
        {
            CurLanguage = lang;
        }

        public DelegateCommand<string> SelectCmd => new DelegateCommand<string>(SelectCmd_Sub);
        private void SelectCmd_Sub(string UIName)
        {
            _ea.GetEvent<ChangeUIViewEvent>().Publish(UIName);
        }
        public DelegateCommand CanSettingCommand => new DelegateCommand(CanSettingCommand_Sub);
        private void CanSettingCommand_Sub()
        {
            _ea.GetEvent<SetCANInitConfigEvent>().Publish();
        }

        private string language;
        public string CurLanguage
        {
            get { return language; }
            set
            {
                SetProperty(ref language, value);
                AppResources.Current.Language = value;
                ChangeLanguageEvent.CurLanguage = language;
                _ea.GetEvent<ChangeLanguageEvent>().Publish(language);
            }
        }

        private string _CurFileName;
        public string CurFileName
        {
            get { return _CurFileName; }
            set
            {
                SetProperty(ref _CurFileName, value);
            }
        }

        /// <summary>
        /// OpenSFMFileCommand
        /// </summary>    
        public DelegateCommand OpenSFMFileCommand => new DelegateCommand(OpenSFMFileCommand_Sub);
        private void OpenSFMFileCommand_Sub()
        {
            Forms.OpenFileDialog open = new Forms.OpenFileDialog();
            open.Multiselect = false;
            open.Filter = $"{LangDic["ParFile"]}(*.SFM)|*.SFM";
            open.Title = $"{LangDic["OpenParFileTitle"]}";

            if (open.ShowDialog() == Forms.DialogResult.OK)
            {
                Task.Run(() =>
                {
                    SFMDocument tmp = SFMDocument.Load(open.FileName);
                    if (tmp != null)
                    {
                        CurFileName = System.IO.Path.GetFileName(open.FileName);
                        SFMDocument.SetSFMDocument(tmp);
                        _ea.GetEvent<OpenSFMIsChangeEvent>().Publish();
                        _ea.GetEvent<SaveSFMOkEvent>().Publish();//解决打开文件时就可以保存问题
                    }
                    else
                    {
                        HCC.Growl.Info($"{LangDic["OpenFailed"]}");
                    }
                });
            }
            else
            {
                HCC.Growl.Info($"{LangDic["OpenFailed"]}");
            }
        }

        public DelegateCommand SaveAsSFMFileCommand => new DelegateCommand(SaveAsSFMFileCommand_Sub);
        private void SaveAsSFMFileCommand_Sub()
        {
            Forms.SaveFileDialog save = new Forms.SaveFileDialog();
            save.Filter = $"{LangDic["ParFile"]}(*.SFM)|*.SFM";
            save.Title = $"{LangDic["SaveFileTitle"]}";

            if (save.ShowDialog() == Forms.DialogResult.OK)
            {
                Task.Run(() =>
                {
                    SFMDocument tmp = SFMDocument.Create();
                    SFMDocument.SaveAs(save.FileName, tmp);
                });
            }
            else
            {
                HCC.Growl.Info($"{LangDic["SaveFailed"]}");
            }
        }

        public DelegateCommand SaveSFMFileCommand => new DelegateCommand(SaveSFMFileCommand_Sub);
        private void SaveSFMFileCommand_Sub()
        {
            _ea.GetEvent<SaveSFMEvent>().Publish();
        }

        protected override void SFMFileIsReLoaded()
        {

        }

        public DelegateCommand ExitCommand => new DelegateCommand(ExitCommand_Sub);
        private void ExitCommand_Sub()
        {
            App.Current.Shutdown();
        }

        public DelegateCommand ConnectCommand => new DelegateCommand(ConnectCommand_Sub);
        private void ConnectCommand_Sub()
        {
            _ea.GetEvent<ConnectCanEvent>().Publish();
        }
        public DelegateCommand DisConnectCommand => new DelegateCommand(DisConnectCommand_Sub);
        private void DisConnectCommand_Sub()
        {
            _ea.GetEvent<DisConnectCanEvent>().Publish();
        }

        public DelegateCommand RefreshCommand => new DelegateCommand(RefreshCommand_Sub);
        private void RefreshCommand_Sub()
        {
            _ea.GetEvent<RefreshCanEvent>().Publish();
           
        }
        public DelegateCommand SaveToSystemCommand => new DelegateCommand(SaveToSystemCommand_Sub);
        private void SaveToSystemCommand_Sub()
        {
            _ea.GetEvent<SaveToCanSystemEvent>().Publish((Properties.Settings.Default.AutoSaveToInherSFM, Properties.Settings.Default.EqualWrite));
        }
        public DelegateCommand GeneralSettingCommand => new DelegateCommand(GeneralSettingCommand_Sub);
        private void GeneralSettingCommand_Sub()
        {
            var win = new GeneralSettingWindow();
            win.Show();
        }


        private const string HMI_HelpFileName = "上位机Help.pdf";
        public DelegateCommand HelpCommand => new DelegateCommand(HelpCommand_Sub);
        private void HelpCommand_Sub()
        {
            try
            {
                string path = System.IO.Path.Combine(AppContext.BaseDirectory, "Help", HMI_HelpFileName);
                System.Diagnostics.Process.Start(path);
            }
            catch (Exception ex)
            {

                HCC.Growl.Info($"{LangDic["OpenFailed"]}  Help/{HMI_HelpFileName}");
            }
        }

        public DelegateCommand CheckUpdatesCommand => new DelegateCommand(CheckUpdatesCommand_Sub);
        private void CheckUpdatesCommand_Sub()
        {
            HCC.Growl.Info($"{LangDic["NotImplemented"]}");
        }






    }
}
 